package com.example.obtaingaode.thread;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.example.obtaingaode.activity.MyApplication;
import com.example.obtaingaode.db.DbEntity;
import com.example.obtaingaode.db.LineEntrty;
import com.example.obtaingaode.di.DaggerDbComponent;
import com.example.obtaingaode.util.ExeclUtil;
import com.example.obtaingaode.util.ExeclUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.objectbox.Box;

/**
 * Created by Ma.Chong on 2017/10/26.
 */

public class SaveExcelThread extends Thread {
    @Inject
    Box<DbEntity> dbEntityBox;
    @Inject
    Box<LineEntrty> lineEntrtyBox;

    Handler mHandler;
    public SaveExcelThread(Handler handler) {
        mHandler=handler;
    }


    @Override
    public void run() {
        super.run();
        DaggerDbComponent.builder().appComponent(MyApplication.getAppComponent()).build().inject
                (this);
//        saveDbEntrtyExl("point.xls");
        saveLineEntrtyExl("line.xls");
    }

    private void saveLineEntrtyExl(String filename) {
//        String saveStr = new ExeclUtil().save(LineEntrty.class, lineEntrtyBox.getAll());
//        if (saveStr != null) {
//           mHandler.sendMessage(mHandler.obtainMessage(0,saveStr));
//        }else {
//            mHandler.sendMessage(mHandler.obtainMessage(-1));
//        }
        ExeclUtils.initExcel(filename,new String[]{"Id","位置信息","日期","编号","时间","类型","是否上传"});
        List<LineEntrty> all = lineEntrtyBox.getAll();
        List<ArrayList<String>> datas=new ArrayList<>();
        for (LineEntrty lineEntrty : all) {
            ArrayList<String> list=new ArrayList<>();
            list.add(""+lineEntrty.getId());
            list.add(lineEntrty.getLocation());
            list.add(lineEntrty.getDate());
            list.add(TextUtils.isEmpty(lineEntrty.getNumber())?"Null":lineEntrty.getNumber());
            list.add(lineEntrty.getTime());
            list.add(TextUtils.isEmpty(lineEntrty.getType())?"Null":lineEntrty.getType());
            list.add(""+lineEntrty.getIsUpload());
            datas.add(list);
        }
        ExeclUtils.writeObjListToExcel(datas,filename);
    }

    private void saveDbEntrtyExl(String fileName) {
//        String saveStr = new ExeclUtil().save(DbEntity.class, dbEntityBox.getAll());
        ExeclUtils.initExcel(fileName, new String[]{"ID","设备编号","设备类型","位置信息","备注信息","用户信息","是否上传"});
        List<ArrayList<String>> data=new ArrayList<>();
        for (DbEntity dbEntity : dbEntityBox.getAll()) {
            ArrayList<String> str=new ArrayList<>();
            str.add(""+dbEntity.getId());
            str.add(dbEntity.getDeviceNo());
            str.add(dbEntity.getDeviceType());
            str.add(dbEntity.getLocation());
            str.add(dbEntity.getRemark());
            str.add(dbEntity.getUserNo());
            str.add(""+dbEntity.getIsSaveNet());
            data.add(str);
        }
        ExeclUtils.writeObjListToExcel( data,fileName);
    }
}
